#include "history.h"

cJSON *history_json_object = NULL;

int history_init()
{
    FILE *fp = fopen(HISTORY_PATH, "r");
    if (fp == NULL)
        fp = fopen(HISTORY_PATH, "w+");

    char history_buff_temp[1024 * 100] = {};
    int len = fread(history_buff_temp, 1, sizeof(history_buff_temp), fp);
    // printf("len = %d\n", len);
    fclose(fp);
    if (len == 0)
    {
        history_json_object = cJSON_CreateObject();
        cJSON_AddArrayToObject(history_json_object, "history");
    }
    else
        history_json_object = cJSON_Parse(history_buff_temp);

    return 0;
}

int history_get_title(char *titles[100])
{
    cJSON *title_array = cJSON_GetObjectItem(history_json_object, "history");
    // printf("%p\n", title_array);
    int num = cJSON_GetArraySize(title_array);
    // printf("num:%d\n", num);
    int j = 0;
    for (int i = 0; i < num; i++)
    {
        strcpy(titles[j++], cJSON_GetObjectItem(cJSON_GetArrayItem(title_array, i), "title")->valuestring);

        if (num == 1) // 只有一条记录不用加\n
            break;

        strcpy(titles[j++], "\n");
    }
    strcpy(titles[j], "");

    return 0;
}

int history_get_content_by_title(char *title, char (*question_content)[1024 * 1], char (*response_content)[1024 * 10])
{
    cJSON *history_array = cJSON_GetObjectItem(history_json_object, "history");

    // 搜索标题， 同一个标题的是一个对话
    cJSON *json_title = NULL;

    int num = cJSON_GetArraySize(history_array);
    for (int i = 0; i < num; i++)
    {
        cJSON *item = cJSON_GetArrayItem(history_array, i);
        if (strcmp(cJSON_GetObjectItem(item, "title")->valuestring, title) == 0) // 后续加上时间判断是否是同一个对话
        {
            json_title = item;
            break;
        }
    }

    if (json_title == NULL)
        return -1;

    cJSON *content_array = cJSON_GetObjectItem(json_title, "content");
    int content_num = cJSON_GetArraySize(content_array);
    for (int i = 0; i < content_num; i++)
    {
        strcpy(question_content[i], cJSON_GetObjectItem(cJSON_GetArrayItem(content_array, i), "question")->valuestring);
        strcpy(response_content[i], cJSON_GetObjectItem(cJSON_GetArrayItem(content_array, i), "response")->valuestring);
    }

    return 0;
}

int history_del_by_title(char *title)
{
    cJSON *history_array = cJSON_GetObjectItem(history_json_object, "history");

    // 搜索标题， 同一个标题的是一个对话
    cJSON *json_title = NULL;

    int num = cJSON_GetArraySize(history_array);
    for (int i = 0; i < num; i++)
    {
        cJSON *item = cJSON_GetArrayItem(history_array, i);
        if (strcmp(cJSON_GetObjectItem(item, "title")->valuestring, title) == 0) // 后续加上时间判断是否是同一个对话
        {
            json_title = item;
            cJSON_DeleteItemFromArray(history_array, i);
            break;
        }
    }
    return 0;
}

int history_set(char *title, char *question, char *response)
{
    cJSON *history_array = cJSON_GetObjectItem(history_json_object, "history");

    // 搜索标题， 同一个标题的是一个对话
    cJSON *json_title = NULL;

    int num = cJSON_GetArraySize(history_array);
    for (int i = 0; i < num; i++)
    {
        cJSON *item = cJSON_GetArrayItem(history_array, i);
        if (strcmp(cJSON_GetObjectItem(item, "title")->valuestring, title) == 0) // 后续加上时间判断是否是同一个对话
        {
            json_title = item;
            break;
        }
    }

    if (json_title == NULL)
    {
        json_title = cJSON_CreateObject();
        cJSON_AddStringToObject(json_title, "title", title);
        cJSON_AddItemToArray(history_array, json_title);
        cJSON_AddArrayToObject(json_title, "content");
    }

    // 获取content数组添加记录
    cJSON *content = cJSON_GetObjectItem(json_title, "content");

    cJSON *object = cJSON_CreateObject();
    cJSON_AddStringToObject(object, "question", question);
    cJSON_AddStringToObject(object, "response", response);
    cJSON_AddItemToArray(content, object);

    return 0;
}

int history_close()
{
    FILE *fp = fopen(HISTORY_PATH, "w+");

    fwrite(cJSON_Print(history_json_object), 1, strlen(cJSON_Print(history_json_object)), fp);
    fclose(fp);
    return 0;
}

// int main(int argc, char const *argv[])
// {
//     char test[20] = "hi";
//     history_set(test);
//     char buff[100] = {};
//     history_get(buff);
//     printf("%s\n",buff);
//     return 0;
// }
